<?php

namespace app\index\controller;

use think\facade\Db;
use think\facade\View;

class ProductsController extends BaseController {

    public function index() {
        $products_category_id = input('id/d', 0);
        $where = [];
        if ($products_category_id > 0)
            $where['products_category_id'] = $products_category_id;

        // 案例分类
        $cateList = Db::name('WebProductsCategory')->select()->toArray();
        $productsList = Db::name('WebProducts')->where($where)->field('id, title, img_url')->order('sort, id')->select()->toArray();
        View::assign([
            'productsList' => $productsList,
            'cateList' => $cateList,
        ]);

        return view();
    }

    public function detail() {
        $id = input('id/d', 0);
        if ($id <= 0) exit('参数错误!');
        $products = Db::name('WebProducts')->where('id', $id)->field(true)->find();
        $products_category_name = Db::name('WebProductsCategory')->where('id', $products['products_category_id'])->value('title');

        $newsList = Db::name('WebNews')
            ->field(true)
            ->order('sort, id desc')->select()->toArray();

        //p($products);
        View::assign([
            'products' => $products,
            'products_category_name' => $products_category_name,
            'newsList' => $newsList,
        ]);
        return view();
    }
}
